_package("gt.page");

_import("caf.ui.Page");

_class("DictPage", Page, function(){

	this._init = function(){
		_super._init.call(this);
	};
	this.create = function(parent){
		var obj = this.createTplElement(parent, "dict.xml");
		this.init(obj);
		return obj;
	};
	this.init = function(){
		_super.init.apply(this, arguments);
		this.initComponents();
		this.initActionElements();
		var self = this;
		$("#dictSub").click (function(){
			self.do_query_dict();
		});
		$("#to_tran").click(function(){
			self.do_toTran();
		});
	};
	this.dispose = function(){
		_super.dispose.apply(this);
	};
	this.do_toTran = function(){
		this._app.navPage("home",{"from" : this._pid});
	}
	this.do_query_dict = function(){
		
		var url = "http://www.google.com/dictionary/json?&sl=en&tl=zh&restrict=pr%2Cde&client=te&q="
		var q = $("#ent").val();
        if(q!=""){
            url = url + q + "&callback=?";
            /*$.getJSON(url,function(data){
                $("#result_zh").empty();
                $("#result_en").empty();
                $("<span>").html("中文解释:").appendTo("#result_zh");
                $("<span>").html("英文解释:").appendTo("#result_en");
                $.each(data.primaries[0].entries[0].entries,function(i,element){
                    
                    $("<li>").html(element.terms[1].text).appendTo("#result_zh");
                    $("<li>").html(element.terms[0].text).appendTo("#result_en");
                });
            });*/
			$.ajax({
				type:"get",
				dataType:"jsonp",
				url : url,
				success : function(data){
					$("#wait1").hide();
					

					$("<span>").html("中文解释:").appendTo("#result_zh");
					$("<span>").html("英文解释:").appendTo("#result_en");
					$.each(data.primaries[0].entries[0].entries,function(i,element){
						
						$("<li>").html(element.terms[1].text).appendTo("#result_zh");
						$("<li>").html(element.terms[0].text).appendTo("#result_en");
					});
					$("#ent").blur();
				},
				beforeSend : function(){
					
					$("#wait1").html('<img src="res/images/wait.png"/>请稍候');
					$("#wait1").show();
					$("#result_en").empty();
					$("#result_zh").empty();
				},
				error : function(){
					$("#wait1").html("出错了,请重试");
				}
			});
        }

		/*
		var url = "http://dict.cn/mini.php?q="
		var q = $E("ent").value;
		if(q!=""){
			url = url + q;
			$E("result_zh").innerHTML="<iframe src='" + url + "'></iframe>"
		}
		*/


	};
});